package Function.ItemUse;

import FCRendering.FCRBattleOrderConstant;
import FCRendering.FCRConstants;
import FCRendering.FCRMaster;
import Function.FCMutilPlayCache;
import VWorldGameStudio.*;
import socket.FCRSocketClient;
import socket.FCRSocketServer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import static FCRendering.FCRMaster.*;

public class FoodAndDrugTypeItemUse extends AItemUse {


    @Override
    public ItemUseResult directUse() {
        return useForPlayerTarget(keepPlayer);
    }

    @Override
    public ItemUseResult useInMapTarget(int x, int y) {
        return new ItemUseResult().useless();
    }

    @Override
    public ItemUseResult useForMonsterTarget(FCBattleOrder battleOrder, FCMonster m) {
        return new ItemUseResult().useless();
    }

    @Override
    public ItemUseResult useForPlayerTarget(FCPlayerInfo p) {
        if(p.isDeath()){
            fcv.getMessage().add("不能给死者使用,什么也没发生");
            fcv.showMessage();
            return new ItemUseResult().useless();
        }

        ItemUseResult result = new ItemUseResult();
        if (useItem.getBloodAdd() > 0) {//TODO 加血了.立刻加血
            int b = p.getBlood() + useItem.getBloodAdd();
            b = Math.min(b, p.getBloodMax());
            b = Math.max(0, b);
            p.setBlood(b);
            result.resultString.add("给 " + p.getChName() + " 回复" + b + "血量");
            fcv.getMessage().add("给 " + p.getChName() + " 回复" + b + "血量");
            fcv.showMessage();
            //战斗中
            if (fcv.isBattle()) {
                //下一回合继续加血
                FCPlayerBattleWrap playerBattleWrap = FCPlayerBattleWrap.findBattleWrapByName(p.getAliasUID(), p.getName());
                if (useItem.getBloodNum() > 1) {
                    ArrayList<FCPlayerBattleWrap> wraps = new ArrayList<>();
                    wraps.addAll(fcv.getBattleWraps());
                    Comparator com2 = new Comparator<FCPlayerBattleWrap>() {
                        @Override
                        public int compare(FCPlayerBattleWrap o1, FCPlayerBattleWrap o2) {
                            FCPlayerInfo p1 = FCMutilPlayCache.findPlayerByName(o1.getHostUid(), o1.getName());
                            FCPlayerInfo p2 = FCMutilPlayCache.findPlayerByName(o2.getHostUid(), o2.getName());
                            int b1 = p1.getBloodMax() - p1.getBlood();
                            int b2 = p2.getBloodMax() - p2.getBlood();
                            return b1 - b2;
                        }
                    };
                    Collections.sort(wraps, com2);
                    wraps.remove(playerBattleWrap);
                    for (int i = 0; i < wraps.size(); i++) {
                        if (i < useItem.getBloodNum()) {
                            FCPlayerBattleWrap w = wraps.get(i);
                            FCPlayerInfo p1 = FCMutilPlayCache.findPlayerByName(w.getHostUid(), w.getName());
                            int bl = p1.getBlood() + useItem.getBloodAdd();
                            bl = Math.min(bl, p1.getBloodMax());
                            bl = Math.max(0, bl);
                            p1.setBlood(bl);
                            fcv.getMessage().add("给 " + p1.getChName() + " 回复" + bl + "血量");
                            fcv.showMessage();
                            if (useItem.getBloodRound() > 1) {
                                //持续回合加血
                                w.addlongTermState(FCRBattleOrderConstant.S_AddBlood, useItem.getBloodAdd(), useItem.getBloodRound());
                            }
                        } else {
                            break;
                        }
                    }
                }
                if (useItem.getBloodRound() > 1) {
                    playerBattleWrap.addlongTermState(FCRBattleOrderConstant.S_AddBlood, useItem.getBloodAdd(), useItem.getBloodRound());
                }
            }
        }

        if(useItem.getPhysicalAdd() != 0){
            //TODO 体力值，可以是加和减，最大是100，最小是0
            int newphysical = p.getPhysicalnum()+useItem.getPhysicalAdd();
            newphysical = Math.max(0, newphysical);
            newphysical = Math.min(p.getPhysical(), newphysical);
            p.setPhysicalnum(newphysical);
            fcv.getMessage().add(p.getChName()+"当前体力值为"+newphysical);
            fcv.showMessage();
        }

        //TODO 第二个用途，解毒或中毒的等
        if(useItem.getElements() != null && !useItem.getElements().equals("null")){
            if (fcv.isBattle()) {
                FCPlayerBattleWrap playerBattleWrap = FCPlayerBattleWrap.findBattleWrapByName(p.getAliasUID(), p.getName());
                if(useItem.getElements().equals(FCRBattleOrderConstant.S_Paralysis)){
                    checkElement(result,playerBattleWrap, FCRBattleOrderConstant.S_Paralysis, useItem.getElementsValue(),useItem.getElementsRound());
                }else if(useItem.getElements().equals(FCRBattleOrderConstant.S_Sleep)){
                    checkElement(result,playerBattleWrap, FCRBattleOrderConstant.S_Sleep, useItem.getElementsValue(),useItem.getElementsRound());
                }else if(useItem.getElements().equals(FCRBattleOrderConstant.S_Fire)){
                    checkElement(result,playerBattleWrap, FCRBattleOrderConstant.S_Fire, useItem.getElementsValue(),useItem.getElementsRound());
                }else if(useItem.getElements().equals(FCRBattleOrderConstant.S_Frozen)){
                    checkElement(result,playerBattleWrap, FCRBattleOrderConstant.S_Frozen, useItem.getElementsValue(),useItem.getElementsRound());
                }else if(useItem.getElements().equals(FCRBattleOrderConstant.S_Acid)){
                    checkElement(result,playerBattleWrap, FCRBattleOrderConstant.S_Acid, useItem.getElementsValue(),useItem.getElementsRound());
                }else if(useItem.getElements().equals(FCRBattleOrderConstant.S_Gas)){
                    checkElement(result,playerBattleWrap, FCRBattleOrderConstant.S_Gas, useItem.getElementsValue(),useItem.getElementsRound());
                }else if(useItem.getElements().equals(FCRBattleOrderConstant.S_Confusion)){
                    checkElement(result,playerBattleWrap, FCRBattleOrderConstant.S_Confusion, useItem.getElementsValue(),useItem.getElementsRound());
                }
            } else {
                if(useItem.getElements().equals(FCRBattleOrderConstant.S_Paralysis)){
                    checkElement(result,p, FCRBattleOrderConstant.S_Paralysis, useItem.getElementsValue(),useItem.getElementsRound());
                }else if(useItem.getElements().equals(FCRBattleOrderConstant.S_Sleep)){
                    checkElement(result,p, FCRBattleOrderConstant.S_Sleep, useItem.getElementsValue(),useItem.getElementsRound());
                }else if(useItem.getElements().equals(FCRBattleOrderConstant.S_Fire)){
                    checkElement(result,p, FCRBattleOrderConstant.S_Fire, useItem.getElementsValue(),useItem.getElementsRound());
                }else if(useItem.getElements().equals(FCRBattleOrderConstant.S_Frozen)){
                    checkElement(result,p, FCRBattleOrderConstant.S_Frozen, useItem.getElementsValue(),useItem.getElementsRound());
                }else if(useItem.getElements().equals(FCRBattleOrderConstant.S_Acid)){
                    checkElement(result,p, FCRBattleOrderConstant.S_Acid, useItem.getElementsValue(),useItem.getElementsRound());
                }else if(useItem.getElements().equals(FCRBattleOrderConstant.S_Gas)){
                    checkElement(result,p, FCRBattleOrderConstant.S_Gas, useItem.getElementsValue(),useItem.getElementsRound());
                }else if(useItem.getElements().equals(FCRBattleOrderConstant.S_Confusion)){
                    checkElement(result,p, FCRBattleOrderConstant.S_Confusion, useItem.getElementsValue(),useItem.getElementsRound());
                }
            }
        }

        if (fcv.isBattle()) {
            FCPlayerBattleWrap playerBattleWrap = FCPlayerBattleWrap.findBattleWrapByName(p.getAliasUID(), p.getName());
            //3 提升命中率
            if(useItem.getHitRate() > 0){
                playerBattleWrap.addlongTermState(FCRBattleOrderConstant.S_AddHitRate, useItem.getHitRate(), useItem.getAttackRound());
            }
            //4 提升攻击力
            if(useItem.getAttackHurt() > 0){
                playerBattleWrap.addlongTermState(FCRBattleOrderConstant.S_AddAttack, useItem.getAttackHurt(), useItem.getAttackRound());
            }
//            //5 提升闪避率
//            if(useItem.getDodgeRateAdd() > 0){
//                playerBattleWrap.addlongTermState(FCRBattleOrderConstant.S_AddDodgeRate, useItem.getDodgeRateAdd(), useItem.getAttackRound());
//            }
            //6 提升速度
            if(useItem.getSpeedAdd() > 0){
                playerBattleWrap.addlongTermState(FCRBattleOrderConstant.S_AddSpeed, useItem.getSpeedAdd(), useItem.getAttackRound());
            }
            //7 提升暴击率
            if(useItem.getCrit() > 0){
                playerBattleWrap.addlongTermState(FCRBattleOrderConstant.S_AddCrit, (int)(useItem.getCrit()*100), useItem.getAttackRound());
            }
        }


        useAfter();

        if (!fcv.isBattle()) {
            ArrayList<FCPlayerInfo> needSyncRemoteBlood = new ArrayList<>();
            needSyncRemoteBlood.add(p);
            if (FCRMaster.fcv.isSever()) {
                FCRSocketServer.getIn().syncPlayerState(needSyncRemoteBlood);
            } else if (FCRMaster.fcv.isClient()) {
                FCRSocketClient.getIn().syncPlayerState(needSyncRemoteBlood);
            }
        }
        return result;
    }

    @Override
    public ItemUseResult useForTankTarget(FCTank t) {
        return new ItemUseResult().useless();
    }
    private void checkElement(ItemUseResult result,FCPlayerInfo wrap, String element, int val, int roud){
        FCLongTermState e = wrap.getLongTermMap().get(element);
        int oldV = -1;
        if (e != null) {
            oldV = e.value;
        }
        if(oldV + val > 0){
            //中了状态
            wrap.getLongTermMap().put(element, new FCLongTermState(element,val, roud));
            result.resultString.add(wrap.getChName()+"受到了"+langLib.getNameOfId(element));
            fcv.getMessage().add(wrap.getChName()+"受到了"+langLib.getNameOfId(element));
            fcv.showMessage();
        }else {
            //解除了这个状态
            wrap.getLongTermMap().remove(element);
            result.resultString.add(wrap.getChName()+"解除了"+langLib.getNameOfId(element));
            fcv.getMessage().add(wrap.getChName()+"解除了"+langLib.getNameOfId(element));
            fcv.showMessage();
        }
    }
    private void checkElement(ItemUseResult result,FCPlayerBattleWrap wrap, String element, int val, int roud){
        FCPlayerInfo p1 = FCMutilPlayCache.findPlayerByName(wrap.getHostUid(), wrap.getName());
       int oldV = wrap.getLongTermVal(element);
       if(oldV + val > 0){
        //中了状态
           wrap.addlongTermState(element, oldV + val, roud);
           result.resultString.add(p1.getChName()+"受到了"+langLib.getNameOfId(element));
           fcv.getMessage().add(p1.getChName()+"受到了"+langLib.getNameOfId(element));
           fcv.showMessage();
       }else {//因为要解除的话，val就是-100了，肯定是负数的
           //解除了这个状态
           wrap.removeLongTermState(element);
           result.resultString.add(p1.getChName()+"解除了"+langLib.getNameOfId(element));
           fcv.getMessage().add(p1.getChName()+"解除了"+langLib.getNameOfId(element));
           fcv.showMessage();
       }
    }
}
